package com.chejianer.push.controller;

import com.chejianer.core.domain.entity.vo.SuccessOrFailResp;
import com.chejianer.core.exception.BusinessException;
import com.chejianer.core.utils.PushCode;
import com.chejianer.push.service.PushMessageBySocketIoService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * Created by Chejianer on 2016/11/1.
 */
@RestController
public class PushMessageController {
    @Autowired
    private PushMessageBySocketIoService pushMessageBySocketIoService;

    @RequestMapping(path = {"/pushBuyer"}, method = {RequestMethod.POST})
    public Object pushToBuyer(@RequestBody PushRequestBody push){
        if(hasEmpty(push)){
            return new BusinessException("请完善推送请求信息！");
        }
        pushMessageBySocketIoService.pushToBuyer(push.getClientId(),push.getMessage(),push.getPushCode());
        return new SuccessOrFailResp(true,"推送成功");
    }

    @RequestMapping(path = {"/pushVendor"}, method = {RequestMethod.POST})
    public Object pushToVendor(@RequestBody PushRequestBody push){
        if(hasEmpty(push)){
            return new BusinessException("请完善推送请求信息！");
        }
        pushMessageBySocketIoService.pushToVendor(push.getClientId(),push.getMessage(),push.getPushCode());
        return new SuccessOrFailResp(true,"推送成功");
    }

    @RequestMapping(path = {"/sendAllBuyer"}, method = {RequestMethod.GET})
    public Object sendAllBuyer(@RequestParam String message,@RequestParam String pushCode){
        pushMessageBySocketIoService.pushToAllBuyer(message,PushCode.valueOf(pushCode));
        return new SuccessOrFailResp(true,"推送成功");
    }
    @RequestMapping(path = {"/sendAllVendor"}, method = {RequestMethod.GET})
    public Object sendAllVendor(@RequestParam String message,@RequestParam String pushCode){
        pushMessageBySocketIoService.pushToAllVendor(message,PushCode.valueOf(pushCode));
        return new SuccessOrFailResp(true,"推送成功");
    }

    @Data
    public static class PushRequestBody{
        private String clientId;
        private String message;
        private PushCode pushCode;
    }

    public Boolean hasEmpty(PushRequestBody pushRequestBody){
        if(StringUtils.isEmpty(pushRequestBody.getClientId())
                ||StringUtils.isEmpty(pushRequestBody.getMessage())
                ||pushRequestBody.getPushCode()==null){
            return true;
        }
        return false;
    }

}
